package com.noname.lib_base_java.net;

import android.os.Environment;

import com.noname.lib_base_java.util.L;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import okhttp3.ResponseBody;

/**
 * Author: Lmoumou
 * Date: 2019-01-18 14:00
 * 文件名: DownloadManager
 * 描述:
 */
public class DownLoadManager {
    private static final String TAG = "DownLoadManager";
    private static String APK_CONTENTTYPE = "application/vnd.android.package-archive";
    private static String PNG_CONTENTTYPE = "image/png";
    private static String JPG_CONTENTTYPE = "image/jpg";
    private static String MP4_CONTENTTYPE = "video/mp4";
    private static String MP3_CONTENTTYPE = "audio/mp3";
    private static String ZIP_CONTENTTYPE = "video/mp3";
    private static String fileSuffix = "";
    private static File sFutureStudioIconFile;
    private static long sFileSize;
    private static String fileName = "GREDownload";

    public static String getFileName() {
        return fileName;
    }

    public static File getFutureStudioIconFile() {
        return sFutureStudioIconFile;
    }

    public static boolean writeFile(ResponseBody body) {

        L.e(TAG, "contentType:>>>>" + body.contentType().toString());
        String type = body.contentType().toString();
        if (type.equals(APK_CONTENTTYPE)) {
            fileSuffix = ".apk";
        } else if (type.equals(PNG_CONTENTTYPE)) {
            fileSuffix = ".png";
        } else if (type.equals(JPG_CONTENTTYPE)) {
            fileSuffix = ".jpg";
        } else if (type.equals(MP4_CONTENTTYPE)) {
            fileSuffix = ".mp4";
        } else if (type.equals(MP3_CONTENTTYPE)) {
            fileSuffix = ".mp3";
        } else {
            fileSuffix = ".zip";
        }

        String sPath = Environment.getExternalStorageDirectory() + File.separator + fileName + fileSuffix;
        L.e(TAG, "path:>>>>" + sPath);
        try {
            // todo change the file location/name according to your needs
            sFutureStudioIconFile = new File(sPath);
            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];
                //需要下载的文件总长度
                if (sFileSize == 0) {
                    sFileSize = body.contentLength();
                }
                //已经下载的文件长度
                long fileSizeDownloaded = sFutureStudioIconFile.length();
                inputStream = body.byteStream();
                outputStream = new FileOutputStream(sFutureStudioIconFile, true);
//                progressBar.setMax((int) sFileSize);
                while (true) {
                    int read = inputStream.read(fileReader);
                    if (read == -1) {
                        break;
                    }
                    outputStream.write(fileReader, 0, read);
                    fileSizeDownloaded += read;
//                    progressBar.setProgress((int) fileSizeDownloaded);
                    L.e(TAG, "file download: " + fileSizeDownloaded + " of " + sFileSize);
                }
                outputStream.flush();

                return true;
            } catch (IOException e) {
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            return false;
        }
    }

}
